<?php
/*
 * This page is for I'm pregnant.
 */
$rp = $_SERVER['REQUEST_URI'];
?>
<head>
    <script src="../../include/js/apa.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../include/js/jquery.timelinr-0.9.5.js" type="text/javascript"></script>
    <link rel="stylesheet" href="../../include/css/style_tl.css" type="text/css" media="screen" />
    <script type="text/javascript"> 
        $(function(){
            $().timelinr()
        });
    </script>

    <script type="text/javascript" language="JavaScript1.2" charset="utf-8">
               
        function isValidDate(dateStr) {
            // Date validation function courtesty of 
            // Savan Koradia (sjk.frills2008@gmail.com) -->
            // Checks for the following valid date formats:
            // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

            var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

            var matchArray = dateStr.match(datePat); // is the format ok?
            if (matchArray == null) {
                alert("Date is not in a valid format.")
                return false;
            }
            month = matchArray[1]; // parse date into variables
            day = matchArray[3];
            year = matchArray[4];
            if (month < 1 || month > 12) { // check month range
                alert("Month must be between 1 and 12.");
                return false;
            }
            if (day < 1 || day > 31) {
                alert("Day must be between 1 and 31.");
                return false;
            }
            if ((month==4 || month==6 || month==9 || month==11) && day==31) {
                alert("Month "+month+" doesn't have 31 days!")
                return false;
            }
            if (month == 2) { // check for february 29th
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day>29 || (day==29 && !isleap)) {
                    alert("February " + year + " doesn't have " + day + " days!");
                    return false;
                }
            }
            return true;
        }

        function dispDate(dateObj) {
            month = dateObj.getMonth()+1;
            month = (month < 10) ? "0" + month : month;

            day   = dateObj.getDate();
            day = (day < 10) ? "0" + day : day;

            year  = dateObj.getYear();
            if (year < 2000) year += 1900;

            return (month + "/" + day + "/" + year);
        }

        function pregnancyCalc(pregform) {
            menstrual = new Date(); // creates new date objects
            ovulation = new Date();
            ovstart = new Date();
            ovend = new Date();
            duedate = new Date();
            today = new Date();
            cycle = 0; // sets variables to invalid state ==> 0

            if (isValidDate(pregform.menstrual.value)) { // Validates menstual date 
                menstrualinput = new Date(pregform.menstrual.value);
                menstrual.setTime(menstrualinput.getTime())
                
                
            }
            
            else return false; // otherwise exits
            setCookie('mens', pregform.menstrual.value, 1);
            cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
            // validates cycle range, from 22 to 45
            if (pregform.cycle.value != "" && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
                alert("Your cycle length is either too short or too long for \n"
                    + "calculations to be very accurate!  We will still try to \n"
                    + "complete the calculation with the figure you entered. ");
            }

            // sets ovulation date to menstrual date + cycle days - 14 days
            // the '*86400000' is necessary because date objects track time
            // in milliseconds;  86400000 milliseconds equals one day
            ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (13*86400000));
            pregform.conception.value = dispDate(ovulation);
            setCookie('cycle', cycle, 1);
            
           

            ovstart.setTime(ovulation.getTime() - (5*86400000));
            pregform.ovstart.value = dispDate(ovstart);
            

            ovend.setTime(ovulation.getTime() + (5*86400000));
            pregform.ovend.value = dispDate(ovend);

            // sets due date to ovulation date plus 266 days
            duedate.setTime(ovulation.getTime() + 266*86400000);
            pregform.duedate.value = dispDate(duedate);
           

            // sets fetal age to 14 + 266 (pregnancy time) - time left
            var fetalage = 14 + 266 - ((duedate - today) / 86400000);
            weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
            days = Math.floor(fetalage % 7); // sets days to the whole number remainder

            // fetal age message, automatically includes 's' on week and day if necessary
            fetalage = weeks + " week" + (weeks > 1 ? "s" : "") + ", " + days + " days";
            pregform.fetalage.value = fetalage;

            setCookie('duedate', dispDate(duedate), 1);
             
            return false; // form should never submit, returns false
        }
        //  End -->
    </script>      
    <script type="text/javascript">
        $(function() {
            $('#menstrual').datepicker({
                numberOfMonths: 3,
                showButtonPanel: true
            });
        });
    </script>

    <script type="text/javascript">
        function changevalue(a){
            $("#"+a).attr("value","");
            $("#"+a).removeClass("hint-value");
        }
        function unhide(a,b){
            $("#"+a).attr("class",b);            
            document.getElementById("timeline").style.display="block";
            document.getElementById("str").style.display="block";
        }
        
    </script>



    <style>
        .required{color: red;}
        #preg-calc .hint { display: block; color:#aaa; font-size: 10px; line-height:1.4;}           
        .hint1{color: #aaa; font-size: 10px; line-height: 1.4; padding-right: 5px;}
        #preg-calc fieldset.hide, #preg-calc fieldset#results.hide { display:none;} /* use javascript function 'unhide' to display results */        
        #art_body{ width: 550px; }
        input{height: 20px;}
        button{width: 75px; height: 35px; background-color: #F6358A; border: none; font-size: 15px;}            
        legend { color : #F6358A; }
    </style>



</head>




<div id="art_body">
    <form onsubmit="return pregnancyCalc(this);" action="" id="preg-calc">
        <fieldset id="ovul_calc"> <legend>Pregnancy Due Date Calculator</legend>

            <table >   
                <tr>
                    <td style="width: 300px;" >
                        First Day of Last Menstrual Period: 
                        <span class="hint">first day of bleeding</span><span class="hint"><em>Required</em></span>
                    </td>
                    <td>
                        <input name="menstrual" id="menstrual" placeholder="MM/DD/YYYY" class="medium" type="text" onfocus="changevalue('menstrual')" />  <strong class="required">*</strong>
                    </td>
                </tr>

                <tr>
                    <td>
                        Average Length of Cycles:
                        <span class="hint">From first day of your period to the <br />first day of your 
                            next period.</span>
                        <span class="hint">Ranges from: 22 to 44. Default = 28</span>
                        <span class="hint"><em>Optional:</em> Leave 28 if unsure.</span>
                    </td>
                    <td>
                        <input name="cycle" id="cycle" value="28" class="small" size="2" maxlength="2" type="text" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button id="calc_button" value="Calculate!" type="submit" onclick="unhide('results','normal')">Calculate!</button> 
                    </td>
                </tr>
            </table>
        </fieldset>      




        <fieldset id="results" class="hide">   
            <legend>Results</legend>      

            <table cellpadding="10" >
                <tr>
                    <td style="width: 300px;" >Estimated Fertility Window:</td>
                    <td style="width: 200px;"><div class="hint1">from:</div>
                        <input name="ovstart" id="ovstart" value="" class="medium" type="text" />


                        <div class="hint1">to:</div>
                        <input name="ovend" id="ovend" value="" class="medium" type="text" /></td>
                </tr>
                <tr>  
                    <td>Estimated Conception Date:</td>
                    <td><input name="conception" id="conception" value="" class="medium"type="text" /></td>
                </tr>
                <tr>

                    <td> Estimated Gestational Age:<span class="hint">how far along your pregnancy is</span></td>
                    <td> <input name="fetalage" id="fetalage" value="" class="medium" type="text" /></td>
                </tr>
                <tr>
                    <td>Estimated Due Date: <span class="hint">only 5% born on their due date!</span></td>
                    <td><input name="duedate" id="duedate" value="" class="medium"  type="text" /></td>
                </tr>
            </table>

        </fieldset>
    </form>

    <div id="str" style="display: none;" >
        <form action="../preg/me.php" method="post">
            <input type="hidden" name="ret" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
            <input type="submit" value="ok"/>
        </form>
    </div>
    <p>This is not a diagnosis.  The calculations that are provided are estimates based on
        averages.</p>


    <?php
    //include 'timeline.php';
    //        include 'nt.php';
    ?>
    <div id="timeline" class="hide" style="display: none;" >
        <iframe src="../preg/nt.php" width="1050" height="675" style="margin: 0 0 0 -210px;" frameBorder="0" ></iframe>    
    </div>
</div>



<?php
if ($_GET['s'] == y) {
    ?>
    <script>
        $(function(){
            
            $('#dl0').dialog({
                width : 200,
                modal : true
            });

        });
    </script>
<div id="dl0" title="Successfull">
    <p>SuccessFull</p>
</div>
    <?php
} elseif ($_GET['s'] == n) {
    ?>
    <script>
        $(function(){
            
            $('#dl00').dialog({
                width : 200,
                modal : true
            });

        });
    </script>
<div id="dl00" title="Unsuccessful">
    <p>Opps...!!! Got some error :( <br/> Try Again...</p>
</div>
<?php } ?>
